<?php
/*************************************************************************************
   Copyright notice

   This script uses some code parts of the glossary module for phpwcms (c) Oliver Georgi
   (c) 2002-2009 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.

   This script uses some code parts from bmcatalog module
   (c) 2010 Benjamin Matzmorr, www.etype.de

   (c) 2012 webrealisierung gmbh, www.webrealisierung.ch
   Catalogue Module V 1.0.0  -  Date: 2012-06-01

   The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
   A copy is found in the textfile GPL.txt and important notices to the license
   from the author is found in LICENSE.txt distributed with these scripts.

   This script is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
   PARTICULAR PURPOSE.  See the GNU General Public License for more details.

   This copyright notice MUST APPEAR in all copies of the script!
*************************************************************************************/
// ----------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {
   die("You Cannot Access This Script Directly, Have a Nice Day.");
}
// ----------------------------------------------------------------


$plugin['query']			= '';
$plugin['filter_array'] = array();
$plugin['filter'] = isset($_POST['filter']) ? clean_slweg($_POST['filter']) : '';

// create pagination
if(isset($_GET['c'])) {
	$_SESSION['ctgbe']['list_count_entry'] = $_GET['c'] == 'all' ? '99999' : intval($_GET['c']);
	if( $_GET['c'] == 'all'){
    $plugin['filter'] = '';
    unset($_SESSION['ctgbe']['filter_entry']);
  }
}
if(isset($_GET['page'])) {
	$_SESSION['ctgbe']['page_entry'] = intval($_GET['page']);
}

// set default values for paginating
if(empty($_SESSION['ctgbe']['list_count_entry'])) {
	$_SESSION['ctgbe']['list_count_entry'] = 25;
}

// paginate and search form processing
if(isset($_POST['do_pagination'])) {

	$_SESSION['ctgbe']['list_active_entry']	= empty($_POST['showactive']) ? 0 : 1;
	$_SESSION['ctgbe']['list_inactive_entry']	= empty($_POST['showinactive']) ? 0 : 1;

	$_SESSION['filter']			= clean_slweg($_POST['filter']);
	if(empty($plugin['filter']) || $plugin['filter'] == '') {
		unset($_SESSION['ctgbe']['filter_entry']);
	} else {
		$_SESSION['ctgbe']['filter_entry']	= convertStringToArray($plugin['filter'], ' ');
//		$_POST['filter']	= $_SESSION['ctgbe']['filter_entry'];
	}
}

	$_SESSION['catalog_category_filter']	= isset($_SESSION['catalog_category_filter']) ? $_SESSION['catalog_category_filter']: '';
	$_SESSION['catalog_category_filter']	= isset($_POST['catalog_category_filter']) ? intval($_POST['catalog_category_filter']): $_SESSION['catalog_category_filter'];


	if(empty($_SESSION['catalog_category_filter'])) {
		unset($_SESSION['catalog_category_filter']);
	}


if(empty($_SESSION['ctgbe']['page_entry'])) {
	$_SESSION['ctgbe']['page_entry'] = 1;
}

$plugin['list_active']		= isset($_SESSION['ctgbe']['list_active_entry'])	? $_SESSION['ctgbe']['list_active_entry']		: 1;
$plugin['list_inactive']	= isset($_SESSION['ctgbe']['list_inactive_entry'])	? $_SESSION['ctgbe']['list_inactive_entry']	: 1;


// set correct status query
if($plugin['list_active'] != $plugin['list_inactive']) {

	if(!$plugin['list_active']) {
		$plugin['query'] .= 'ctge_status=0';
	}
	if(!$plugin['list_inactive']) {
		$plugin['query'] .= 'ctge_status=1';
	}

} else {
	$plugin['query'] .= 'ctge_status!=9';
}

if(isset($_SESSION['ctgbe']['filter_entry']) && is_array($_SESSION['ctgbe']['filter_entry']) && count($_SESSION['ctgbe']['filter_entry'])) {

	foreach($_SESSION['ctgbe']['filter_entry'] as $value) {
		//usr_name, usr_login, usr_email
		$plugin['filter_array'][] = "CONCAT(ctge_title, ctge_text) LIKE '%".aporeplace($value)."%'";
	}
	if(count($plugin['filter_array'])) {
		$plugin['query'] .= ' AND ('.implode(' OR ', $plugin['filter_array']).')';
	}

}

//Categoryfilter
if (isset($_SESSION['catalog_category_filter']) && intval($_SESSION['catalog_category_filter'])) {
	$plugin['query'] .= " AND ctge_catalogid LIKE '%|".$_SESSION['catalog_category_filter']."|%' ";
}




// paginating values
$plugin['count_total'] = _dbQuery('SELECT * FROM '.DB_PREPEND.'phpwcms_mod_catalogue_entry WHERE '.$plugin['query'], 'COUNT');
$plugin['pages_total'] = ceil($plugin['count_total'] / $_SESSION['ctgbe']['list_count_entry']);
if($_SESSION['ctgbe']['page_entry'] > $plugin['pages_total']) {
	$_SESSION['ctgbe']['page_entry'] = empty($plugin['pages_total']) ? 1 : $plugin['pages_total'];
}



?>

<div class="navBarLeft imgButton chatlist" style="float:left">
	&nbsp;&nbsp;
	<a href="<?php echo MODULE_HREF ?>&amp;edit=0" title="<?php echo $BLM['ctg_create_new'] ?>"><img src="<?php echo $phpwcms['modules'][$module]['dir'] ?>img/page_white_add.gif" alt="Add" border="0" /><span><?php echo $BLM['ctg_create_new'] ?></span></a>
</div>
<div style="clear:both"></div>

<form action="<?php echo MODULE_HREF ?>" method="post" name="catalogentries" id="catalogentries"><input type="hidden" name="do_pagination" value="1" />
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="paginate" summary="">
	<tr>
		<td><table border="0" cellpadding="0" cellspacing="0" summary="">
			<tr>

				<td><input type="checkbox" name="showactive" id="showactive" value="1" onclick="this.form.submit();"<?php is_checked(1, $plugin['list_active'], 1) ?> /></td>
				<td><label for="showactive"><img src="img/button/aktiv_12x13_1.gif" alt="" style="margin:1px 1px 0 1px;" /></label></td>
				<td><input type="checkbox" name="showinactive" id="showinactive" value="1" onclick="this.form.submit();"<?php is_checked(1, $plugin['list_inactive'], 1) ?> /></td>
				<td><label for="showinactive"><img src="img/button/aktiv_12x13_0.gif" alt="" style="margin:1px 1px 0 1px;" /></label></td>

<?php
if($plugin['pages_total'] > 1) {

	echo '<td class="chatlist">|&nbsp;</td>';
	echo '<td>';
	if($_SESSION['ctgbe']['page_entry'] > 1) {
		echo '<a href="'.MODULE_HREF.'&amp;page='.($_SESSION['ctgbe']['page_entry']-1).'">';
		echo '<img src="'.$phpwcms['modules'][$module]['dir'].'img/action_back.gif" alt="" border="0" /></a>';
	} else {
		echo '<img src="'.$phpwcms['modules'][$module]['dir'].'img/action_back.gif" alt="" border="0" class="inactive" />';
	}
	echo '</td>';
	echo '<td><input type="text" name="page" id="page" maxlength="4" size="4" value="'.$_SESSION['ctgbe']['page_entry'];
	echo '"  class="textinput" style="margin:0 3px 0 5px;width:30px;font-weight:bold;" /></td>';
	echo '<td class="chatlist">/'.$plugin['pages_total'].'&nbsp;</td>';
	echo '<td>';
	if($_SESSION['ctgbe']['page_entry'] < $plugin['pages_total']) {
		echo '<a href="'.MODULE_HREF.'&amp;page='.($_SESSION['ctgbe']['page_entry']+1).'">';
		echo '<img src="'.$phpwcms['modules'][$module]['dir'].'img/action_forward.gif" alt="" border="0" /></a>';
	} else {
		echo '<img src="'.$phpwcms['modules'][$module]['dir'].'img/action_forward.gif" alt="" border="0" class="inactive" />';
	}
	echo '</td><td class="chatlist">&nbsp;|&nbsp;</td>';

} else {

	echo '<td class="chatlist">|&nbsp;<input type="hidden" name="page" id="page" value="1" /></td>';

}
?>
				<td><input type="text" name="filter" id="filter" size="10" value="<?php

				if(isset($_SESSION['ctgbe']['filter_entry']) && is_array($_SESSION['ctgbe']['filter_entry']) ) {
					echo html_specialchars(implode(' ', $_SESSION['ctgbe']['filter_entry']));
				}

				?>" class="textinput" style="margin:0 2px 0 0;width:110px;text-align:left;" title="filter results by username, name or email" /></td>
				<td><input type="image" name="gofilter" src="img/famfamfam/action_go.gif" style="margin-right:3px;" /></td>

				<td>&nbsp;&nbsp;

<?php
    $sql = "SELECT * FROM ".DB_PREPEND."phpwcms_mod_catalogue_catalog WHERE ctgc_status!=9 ORDER BY ctgc_name ASC";
    $data = _dbQuery($sql);
	$xml = "";
  $xml .= "<option value=''>- ".$BLM['cp_catalogs']." -</option>";
  foreach($data as $row) {
	 		$xml .=	'<option value="'.$row['ctgc_id'].'"';
	 		if (isset($_SESSION['catalog_category_filter']) && $row['ctgc_id']==$_SESSION['catalog_category_filter']) {
          $xml  .= ' selected="selected"';
      }
      //$xml .= '>'.$row['ctgc_name'];
      if ( strlen($row['ctgc_name']) > 20 ) {
        $xml .= '>'. substr($row['ctgc_name'], 0, 20) . '&hellip;';
      } else {
        $xml .= '>'. $row['ctgc_name'];
      }
      if ($row['ctgc_status']==0)  $xml .= ' ['.$BLM['disabled'].']';
      $xml .= '</option>';
	}
 ?>

            <select class="chatlist" id="catalog_category_filter" name="catalog_category_filter" onchange='catalogentries.submit()'>
              <?php echo $xml; ?>
            </select>
				</td>
				<td></td>
			</tr>
		</table></td>

	<td class="chatlist" align="right">
		<a href="<?php echo MODULE_HREF ?>&amp;c=25">25</a>
		<a href="<?php echo MODULE_HREF ?>&amp;c=50">50</a>
		<a href="<?php echo MODULE_HREF ?>&amp;c=100">100</a>
		<a href="<?php echo MODULE_HREF ?>&amp;c=250">250</a>
		<a href="<?php echo MODULE_HREF ?>&amp;c=all"><?php echo $BL['be_ftptakeover_all'] ?></a>
	</td>

	</tr>
</table>
</form>

<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="">

	<tr><td colspan="5"><img src="img/leer.gif" alt="" width="1" height="3"></td></tr>
	<tr><td colspan="5" bgcolor="#92A1AF"><img src="img/leer.gif" alt="" width="1" height="1"></td></tr>

<?php
// loop listing available newsletters
$plugin['row_count'] = 0;

$sql  = 'SELECT * FROM '.DB_PREPEND.'phpwcms_mod_catalogue_entry WHERE '.$plugin['query'].' ';
$sql .= 'ORDER BY ctge_title ';
$sql .= 'LIMIT '.(($_SESSION['ctgbe']['page_entry']-1) * $_SESSION['ctgbe']['list_count_entry']).','.$_SESSION['ctgbe']['list_count_entry'];
$plugin['data'] = _dbQuery($sql);

foreach($plugin['data'] as $row) {
  echo '<tr'.( ($plugin['row_count'] % 2) ? ' bgcolor="#F3F5F8"' : '' ).' onmouseover="this.bgColor=\'#CCFF00\';" onmouseout="this.bgColor=\''.( ($plugin['row_count'] % 2) ? '#F3F5F8' : '' ).'\';">'.LF.'<td width="20" style="width:20px;padding:2px 1px 2px 3px;">';

	echo '<img src="'.$phpwcms['modules'][$module]['dir'].'img/page_white.gif" alt="'.$BLM['ctg_entry'].'" /></td>'.LF;

//	echo '<td class="dir" width="25%">'.html_specialchars($row["ctge_category"])."&nbsp;</td>\n";

	echo '<td class="dir">'.html_specialchars($row["ctge_title"])."&nbsp;</td>\n";
	echo '<td class="dir">'.html_specialchars($row["ctge_title2"])."&nbsp;</td>\n";
	echo '<td align="right" nowrap="nowrap" class="button_td">';

  if ($_SESSION['wcs_user_id'] == $row["ctge_userId"] || $_SESSION["wcs_user_admin"] == 1)
  {

	echo '<a href="'.MODULE_HREF.'&amp;edit='.$row["ctge_id"].'">';
	echo '<img src="'.$phpwcms['modules'][$module]['dir'].'img/edit_22x13.gif" border="0" alt="" /></a>';

	echo '<a href="'.MODULE_HREF.'&amp;editid='.$row["ctge_id"].'&amp;verify='.$row["ctge_id"].'-'.$row["ctge_status"].'">';
//	echo (($row["ctge_status"]) ? '0' : '1').'">';
	echo '<img src="'.$phpwcms['modules'][$module]['dir'].'img/aktiv_12x13_'.$row["ctge_status"].'.gif" border="0" alt="" /></a>';

	echo '<a href="'.MODULE_HREF.'&amp;delete='.$row["ctge_id"];
	echo '" title="delete: '.html_specialchars($row["ctge_title"]).'"';
	echo ' onclick="return confirm(\''.$BLM['ctg_delete_entry'].' '.js_singlequote($row["ctge_title"]).'\');">';
	echo '<img src="'.$phpwcms['modules'][$module]['dir'].'img/trash_13x13_1.gif" border="0" alt=""></a>';

  }

	echo "</td>\n</tr>\n";

	$plugin['row_count']++;
}

if($plugin['row_count']) {
	echo '<tr><td colspan="5" bgcolor="#92A1AF"><img src="img/leer.gif" alt="" width="1" height="1"></td></tr>';
}

?>

	<tr><td colspan="5"><img src="img/leer.gif" alt="" width="1" height="15"></td></tr>
</table>